<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title></title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta http-equiv="Access-Control-Allow-Origin" content="*">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">

    <link rel="stylesheet" href="/layui/css/layui.css">
    <link rel="stylesheet" href="/hg-layui-admin-ui/css/font.css">
    <script type="text/javascript" src="/js/jquery-1.8.3.min.js"></script>
    <script type="text/javascript" src="/layui/layui.js"></script>
    <script src="/ajaxfileupload.js"></script>

    <style>

    </style>
</head>

<body class="layui-body-content">
<div class="layui-fluid">
    <form class="layui-form" id="cxform" action="" style="margin: 1.5% 0% 0% 0%;">
        <div class="layui-colla-item" style="margin-left: 2%">
            <div class="layui-inline">
                <label class="layui-form-label" style="margin-left: -50px"><span >教师编号</span></label>
                <div  class="layui-input-inline">
                    <input type="text" id="jsxh" name="jsxh" class="layui-input" placeholder="教师编号" >
                </div>
            </div>

            <div class="layui-inline">
                <label class="layui-form-label" ><span >教师姓名</span></label>
                <div class="layui-input-inline">
                    <input type="text"  name="jsxm" id="jsxm" class="layui-input" placeholder="教师姓名" >
                </div>
            </div>

            <div class="layui-inline">
                <label class="layui-form-label" ><span >课程</span></label>
                <div class="layui-input-inline">
                    <input type="text"  name="kc" id="kc" class="layui-input" placeholder="课程" >
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label" ><span >职称</span></label>
                <div class="layui-input-inline">
                    <input type="text" id="zc" name="zc" class="layui-input" placeholder="职称" >
                </div>
            </div>

            <div class="layui-input-inline" style="margin-left:40px;margin-top: 5px;width: 400px;">
                <button class="layui-btn layui-btn-normal layui-bg-green layui-btn-sm" id="cxbtn" type="button" lay-submit="" lay-filter="cxbtn">查询</button>
                <button class="layui-btn layui-btn-normal layui-btn-sm" id="xzbtn" type="button" lay-submit="" lay-filter="xzbtn">新增</button>
                <button class="layui-btn layui-btn-normal layui-btn-sm" id="xzmb"  style="background: #5FB878"  type="button" lay-submit="xzmb" lay-filter="xzmb">下载模板</button>
                <input id="excelFile" name="excelFile" type="file" onchange="saveFile(this);" style="display:none"  />
                <button  id="drbtn" style="margin-left:2%;background: #5FB878;" onClick="document.getElementById('excelFile').click()" class="layui-btn layui-btn-normal layui-btn-sm"  type="button" >导 入</button>

            </div>

        </div>

        <table style="border: 1px solid red" class="layui-hide" id="test" lay-filter="test"></table>

    </form>
    <script type="text/html" id="bar">
        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    </script>

    <div id="addyhdiv" style="display: none;border-radius: 6px">
        <form class="layui-form" id="formqrmain" action="" style="margin-top: 50px;margin-left:4%;">

            <input type="hidden" id="jsid"  name="jsid">

            <!-- 新增  -->
            <div class="layui-form-item" >
                <label class="layui-form-label" style="width: 65px;"><span  >教师编号</span><span style="color: red">*</span></label>
                <div class="layui-input-inline">
                    <input type="text" id="jsxhs" name="jsxhs"  style="width: 270px;" placeholder="教师编号" class="layui-input qyzzlname" >
                </div>

                <label class="layui-form-label" style="width: 170px"><span id="qyzzlmksjorfr">教师姓名</span><span style="color: red">*</span></label>
                <div class="layui-input-inline">
                    <input type="text" id="jsxms"   name="jsxms" style="width: 270px;"  placeholder="学生姓名"  class="layui-input" />
                </div>
            </div>

            <div class="layui-form-item" >
                <div class="layui-inline" style="width:50%" >
                    <label class="layui-form-label"  style="width: 60px">性别</label>
                    <div class="layui-input-block">
                        <input type="radio" name="jsxb" value="男" title="男  " checked="">
                        <input type="radio" name="jsxb" value="女" title="女   " >
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label" style="width: 39px"><span>密码</span><span style="color: red">*</span></label>
                    <div class="layui-input-inline">
                        <input type="text" id="password"  style="width: 270px;"  name="password"   placeholder="密码"  class="layui-input" />
                    </div>
                </div>


            </div>

            <div class="layui-form-item" >

                <label class="layui-form-label" style="width: 65px;"><span id="qyzzlsfzhorgddh">课程</span><span style="color: red">*</span></label>
                <div class="layui-input-inline" style="width: 270px;">
                    <select id="jskc"  lay-search="" name="jskc" >

                    </select>
                </div>

                <div class=""  id="qyzzllxdz" >
                    <label class="layui-form-label" style="width: 90px;">职务</label>
                    <div class="layui-input-inline">
                        <input type="text" id="jszw"  name="jszw" placeholder="职务" style="width: 270px;"  class="layui-input"  >
                    </div>
                </div>
            </div>

            <div class="layui-form-item" >

                <label class="layui-form-label" style="width: 65px;"><span >邮箱</span></label>
                <div class="layui-input-inline">
                    <input type="email" id="email" name="email" style="width: 270px;"   placeholder="邮箱" class="layui-input" >
                    <!-- lay-verify="required"  -->
                </div>
                <label class="layui-form-label" style="width: 170px;">联系电话</label>
                <div class="layui-input-inline">
                    <input type="text" id="lxdh"  name="lxdh" placeholder="联系电话" style="width: 270px;"  class="layui-input"  >
                </div>
            </div>
            <div class="layui-form-item" >
                <label class="layui-form-label" style="width: 65px;">身份证</label>
                <div class="layui-input-inline">
                    <input type="text" id="sfz"  name="sfz" placeholder="身份证" style="width: 270px;"  class="layui-input"  >
                </div>
                <label class="layui-form-label" style="width:170px;"><span >住址</span></label>
                <div class="layui-input-inline">
                    <input type="text" id="address" name="address"  style="width: 270px;"   placeholder="住址" class="layui-input" >
                    <!-- lay-verify="required"  -->
                </div>
            </div>

        </form>
    </div>
    <script type="text/html" id="toolbarDemo">
        <div class="layui-btn-container">
            <button  type="button" class="layui-btn layui-btn-sm layui-bg-red" lay-event="getCheckData">批量删除</button>
        </div>
    </script>
</div>
</body>
<script>



    $(function() {
        getAllStudents();
        getSelect();
    });

    $("#cxbtn").click(function(){
        getAllStudents();
    });

    function getAllStudents(){
        var number =  $("#jsxh").val();
        var name =  $("#jsxm").val();
        var kc =  $("#kc").val();
        var zc =  $("#zc").val();
        $.ajax({
            type: "POST",
            url: "getAllTeachers",
            data:{number:number,name:name,kc:kc,zc:zc},
            dataType: "json",
            success: function (data) {
                layui.use('table', function(){
                    var table = layui.table;
                    table.render({
                        elem: '#test',
                        // url:"getAllStudents?number=00&name=李&sex=男&rxnj=2019",
                        data:data.rows,
                        height:550,
                        toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
                        ,defaultToolbar: ['', '', '', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
                            title: '提示'
                            ,layEvent: 'LAYTABLE_TIPS'
                            ,icon: 'layui-icon-tips'
                        }],
                        cols: [
                            [
                                {type:'checkbox'}
                                ,{field:'id', width:180, title: 'ID', hide:true ,sort: true}
                                ,{field:'number', width:180, title: '教师编号', sort: true}
                                ,{field:'name', width:120, title: '教师姓名', sort: true}
                                ,{field:'sex', width:120,title: '性别', sort: true}
                                ,{field:'password', width:120, title: '密码', sort: true}
                                ,{field:'ktmc', width:120, title: '课程', sort: true}
                                ,{field:'zw', width:150, title: '职称', sort: true}
                                ,{field:'email', width:210, title: '邮箱', sort: true}
                                ,{field:'phone', width:210, title: '联系电话', sort: true}
                                ,{field:'sfz', width:210, title: '身份证', sort: true}
                                ,{field:'address', width:170, title: '住址', sort: true}
                                ,{fixed: 'right', width:150, align:'center', toolbar: '#bar'}
                            ]
                        ]
                        ,page: true
                    });

                });


            },
            error: function (data, type, err) {
                console.log("ajax错误类型：" + type);
                console.log(err);
                layer.msg('删除失败！');
            }
        });
    }


    //导入excel aaa----------------------------------------
    function saveFile(obj){

        var files = obj.files;
        for (i = 0; i < files.length; i++) {
            file = files[i]
            console.log(file);
        }
        $.ajaxFileUpload({
            url : "exportjs",
            secureuri : false,//安全协议
            type : 'post',
            dataType : 'json',
            fileElementId : "excelFile",
            async : false,//同步请求
            success : function(obj) {
                console.log("返回值");
                console.log(obj);
                if(obj.code == '0000'){
                    layer.msg("导入成功!");
                    setTimeout(function(){
                        location.reload();
                    },1000);

                }else{
                    layer.msg(obj.msg);
                    setTimeout(function(){
                        location.reload();
                    },1500);
                }


            }
        });
    }


    function getSelect(){
        $.ajax({
            type:"POST",
            url:"/admin/ktinfo/getAllKtinfo",
            data:{ktmc:""},
            dataType:"json",
            async : true, //是否异步
            success:function (data) {

                var str = "<option value=''>请选择课堂</option> ";
                if(data.rows.length>0){
                    var datas = data.rows;
                    for (let i = 0; i < datas.length ; i++) {
                        str += "<option value="+datas[i].id+">"+datas[i].ktmc+"</option>";
                    }
                }else{
                    str = "<option value=''>暂无课堂信息</option> ";
                }
                $("#jskc").html(str);

            },
            error: function (data,type, err) {
                console.log("ajax错误类型："+type);
                console.log(err);
                layer.msg('删除失败！');
            }
        });
    };


    layui.use(['table','form'], function() {
        var table = layui.table;
        var layer = layui.layer;
        var form = layui.form;

        //下载模板
        form.on('submit(xzmb)',function(){
            layer.confirm('确定下载模板',{
                title:"提示信息"
            }, function(index) {
                $.ajaxFileUpload({
                    url : "export2",
                    secureuri : false,//安全协议
                    type : 'post',
                    dataType : 'json',
                    fileElementId : "excelFile",
                    async : false,//同步请求
                    success : function(obj) {
                    }
                });
                layer.close(index);
            });

        });

        form.on('submit(xzbtn)',function () {
            var addlayer = layer.open({
                type : 1,
                title : "新增教师",
                btn:['确定','取消'],
               // area : [ '51%', '62%' ],
                area : [ '865px', '500px' ],
                shadeClose : false, //点击遮罩关闭
                content : $('#addyhdiv'),
                success : function() {
                    document.getElementById("formqrmain").reset();
                    layui.form.render();
                    return;
                },
                yes:function(index,layero){
                    var jsxhs = $("#jsxhs").val();
                    var jsxms = $("#jsxms").val();
                    var password = $("#password").val();
                    var jsxb = $("input[name='jsxb']:checked").val();//性别
                    var jskc = $("#jskc").val();
                    var jszw =  $("#jszw").val();
                    var lxdh =  $("#lxdh").val();
                    var email =  $("#email").val();
                    var sfz =  $("#sfz").val();
                    var address =  $("#address").val();


                    if(jsxhs == null || jsxhs == ""){
                        layer.msg("教师编号不能为空！");
                        return;
                    }
                    if(jsxms == null || jsxms == ""){
                        layer.msg("教师姓名不能为空！");
                        return;
                    }
                    if(password == null || password == ""){
                        layer.msg("登录密码不能为空！");
                        return;
                    }

                    if(password!=null){
                        if(password.length<6){
                            layer.msg("登录密码不能小于6位！");
                            return;
                        }
                    }
                    if(jskc == null || jskc == ""){
                        layer.msg("请选择教师课程！");
                        return;
                    }
                    var mobilevalid = /^[1]([3-9])[0-9]{9}$/;
                    if (lxdh!=''&&!mobilevalid.test(lxdh)) {
                        layer.msg("请输入正确的手机号码！");
                        return false;
                    }

                    if(sfz!=''){
                        var reg15 = /^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/; //身份证号码正则
                        if (!reg15.test(sfz)) {
                            layer.msg("请输入正确的身份证号！");
                            return false;
                        }
                    }
                    console.log("打印值");
                    var student ={
                        number : jsxhs,
                        name : jsxms,
                        password : password,
                        kc : jskc,
                        zw : jszw,
                        phone : lxdh,
                        sex : jsxb,
                        email : email,
                        sfz : sfz,
                        address : address,
                    };
                    $.ajax({
                        type:"POST",
                        url:"add",
                        data:student,
                        success:function (data) {
                            layer.msg("增加成功");
                            layer.close(addlayer);
                            getAllStudents();
                        },
                        error: function (data,type, err) {
                            console.log("ajax错误类型："+type);
                            console.log(err);

                            layer.alert("增加失败", {
                                    icon: 6
                                },
                                function () {
                                });
                        }
                    });

                },
                cancel : function() {
                    return;
                },
                end : function() {
                    //$('#main').css("display", "none");
                }
            });


        })


        //头工具栏事件
        table.on('toolbar(test)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            var data = checkStatus.data;
            if(data.length==0){
                layer.msg("请选择数据！");
                return false;
            }
            layer.confirm('是否批量删除',{
                title:"提示信息"
            }, function(index) {
                var str ="";

                for (let i = 0; i <data.length ; i++) {
                    if(i==data.length-1){
                        str += " '"+data[i].id+"' ";
                    }else{
                        str += " '"+data[i].id+"', ";
                    }
                }
                $.ajax({
                    type:"POST",
                    url:"deletes",
                    data:{"ids":str},
                    dataType:"json",
                    success:function (data) {
                        if(data === true){//删除成功：移除删除行
                            layer.msg('提示：删除成功！');
                            getAllStudents();
                        }else{//删除失败
                            layer.msg('提示：删除失败！');
                        };
                    },
                    error: function (data,type, err) {
                        console.log("ajax错误类型："+type);
                        console.log(err);
                        hg.msg('删除失败！');
                    }
                });

                layer.close(index);
            });

            return false;
        });



        table.on('tool(test)', function(obj){
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event;
            if(layEvent === 'edit'){
                var addlayer = layer.open({
                    type : 1,
                    title : "编辑教师信息",
                    btn:['确定','取消'],
                    area : [ '865px', '500px' ],
                   // area : [ '51%', '62%' ],
                    shadeClose : false, //点击遮罩关闭
                    content : $('#addyhdiv'),
                    success : function() {
                        $("#jsxhs").val(data.number);
                        $("#jsxms").val(data.name);
                        $("#password").val(data.password);

                        if(data.sex=='男'){
                            $(":radio[name='jsxb'][value= '男']").prop("checked", true);
                        }else{
                            $(":radio[name='jsxb'][value= '女']").prop("checked", true);
                        }
                        $("#jskc").val(data.kc);
                        $("#jszw").val(data.zw);
                        $("#lxdh").val(data.phone);
                        $("#email").val(data.email);
                        $("#sfz").val(data.sfz);
                        $("#jsid").val(data.id);
                        $("#address").val(data.address);
                        layui.form.render();
                        return;
                    },
                    yes:function(index,layero){
                        var id = $("#jsid").val();
                        var jsxhs = $("#jsxhs").val();
                        var jsxms = $("#jsxms").val();
                        var password = $("#password").val();
                        var jsxb = $("input[name='jsxb']:checked").val();//性别
                        var jskc = $("#jskc").val();
                        var jszw =  $("#jszw").val();
                        var lxdh =  $("#lxdh").val();
                        var email =  $("#email").val();
                        var sfz =  $("#sfz").val();
                        var address =  $("#address").val();


                        if(jsxhs == null || jsxhs == ""){
                            layer.msg("教师编号不能为空！");
                            return;
                        }
                        if(jsxms == null || jsxms == ""){
                            layer.msg("教师姓名不能为空！");
                            return;
                        }
                        if(password == null || password == ""){
                            layer.msg("登录密码不能为空！");
                            return;
                        }

                        if(password!=null){
                            if(password.length<6){
                                layer.msg("登录密码不能小于6位！");
                                return;
                            }
                        }
                        if(jskc == null || jskc == ""){
                            layer.msg("请选择教师课程！");
                            return;
                        }
                        var mobilevalid = /^[1]([3-9])[0-9]{9}$/;
                        if (lxdh!=''&&!mobilevalid.test(lxdh)) {
                            layer.msg("请输入正确的手机号码！");
                            return false;
                        }

                        if(sfz!=''){
                            var reg15 = /^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/; //身份证号码正则
                            if (!reg15.test(sfz)) {
                                layer.msg("请输入正确的身份证号！");
                                return false;
                            }
                        }
                        console.log("打印值");
                        var student ={
                            id:id,
                            number : jsxhs,
                            name : jsxms,
                            password : password,
                            kc : jskc,
                            zw : jszw,
                            phone : lxdh,
                            sex : jsxb,
                            email : email,
                            sfz : sfz,
                            address : address,
                        };

                        $.ajax({
                            type:"POST",
                            url:"edit",
                            data:student,
                            success:function (data) {
                                layer.msg("修改成功");
                                layer.close(addlayer);
                                getAllStudents();
                            },
                            error: function (data,type, err) {
                                console.log("ajax错误类型："+type);
                                console.log(err);

                                layer.alert("修改失败", {
                                        icon: 6
                                    },
                                    function () {
                                    });
                            }
                        });

                    },
                    cancel : function() {
                        return;
                    },
                    end : function() {
                        //$('#main').css("display", "none");
                    }
                });
            }else if(layEvent === 'del'){
                layer.confirm('是否删除',{
                    title:"提示信息"
                }, function(index) {

                    $.ajax({
                        type:"POST",
                        url:"delete",
                        data:{"id":data.id},
                        dataType:"json",
                        success:function (data) {
                            if(data === true){//删除成功：移除删除行
                                layer.msg('提示：删除成功！');
                                getAllStudents();
                            }else{//删除失败
                                layer.msg('提示：删除失败！');
                            };
                        },
                        error: function (data,type, err) {
                            console.log("ajax错误类型："+type);
                            console.log(err);
                            hg.msg('删除失败！');
                        }
                    });

                    layer.close(index);
                });

            }



        })

    })




</script>

</html>